package api

import (
	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/EDDYCJY/go-gin-example/pkg/gredis"
	"github.com/EDDYCJY/go-gin-example/routers/internal"
	"github.com/gomodule/redigo/redis"

	"github.com/gin-gonic/gin"
)

// 查询价格
func Handle_InviteCode(c *gin.Context) {
	appG := app.Gin{C: c}
	invite_code := c.Query("invite_code")
	if invite_code == "" {
		appG.ResponseError(-1, "invite_code is empty")
		return
	}

	conn := gredis.RedisConn.Get()
	defer conn.Close()
	remainingCounts, err := redis.Int(conn.Do("HGET", internal.InviteCodesCountHash, invite_code))
	if err != nil {
		appG.ResponseError(-1, err.Error())
		return
	}
	if remainingCounts <= 0 {
		remainingCounts = 0
	}
	appG.ResponseSuccess(gin.H{
		"reamian_invite_code": remainingCounts,
	})
}
